<template>
	<view>
		<uni-forms-item v-if="fieldConfig.type && ['batch',].includes(fieldConfig.type) && (typeof(fieldConfig?.options?.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required">
			<view class="snowy-shadow" style="margin-bottom: 20rpx;padding: 20rpx; background-color: #f5fff0" v-for="(item, index) in localFieldData" :key="`v-${ index }`">
				<uni-row v-if="typeof(fieldConfig?.options?.disabled) === 'boolean' ? !fieldConfig.options.disabled : true">
					<uni-col :span="24">
						<view class="snowy-flex-end">
							<icon type="clear" :size="20" color="#5677fc" @click="del(index)"></icon>
						</view>
					</uni-col>
				</uni-row>
				<uni-forms :ref="`batch-form-ref-${ fieldConfig.name }-${ index }`" :model="item" label-position="top" labelWidth="auto" validate-trigger="submit">
					<snowy-form-item 
						v-for="(fItem, fIndex) in fieldConfig.list" 
						:key="`sfi-${ fIndex }`" 
						:fieldConfig="fItem" 
						v-model:fieldData="item[fItem?.name]" 
						@fieldDataChange="(value) => snowyFormItemFieldDataUpdate(value, index, fItem?.name)" 
						:useDefault="useDefault" />
				</uni-forms>
			</view>
			<tui-button v-if="typeof(fieldConfig?.options?.disabled) === 'boolean' ? !fieldConfig.options.disabled : true" margin="50rpx 0 0 0" :preventClick="true" type="primary" @click="add"> 增加{{ fieldConfig.label }}
			</tui-button>
		</uni-forms-item>
	</view>
</template>
<script setup>
	import { ref, watch, getCurrentInstance } from 'vue'
	const emits = defineEmits(['update:modelValue'])
	const { proxy } = getCurrentInstance()
	const props = defineProps({
		fieldConfig: {
			type: Object,
			required: true
		},
		modelValue: {
			required: true
		},
		useDefault: {
			type: Boolean,
			default: false,
			required: false
		}
	})
	const localFieldData = ref([])
	watch(() => props.modelValue, (newValue, oldValue) => {
		if (newValue && JSON.stringify(newValue) !== JSON.stringify(localFieldData.value)) {
			localFieldData.value = JSON.parse(JSON.stringify(newValue))
		}
	}, {
		deep: true,
		immediate: true
	})
	const snowyFormItemFieldDataUpdate = (value, index, name) => {
		localFieldData.value[index][name] = value
		emits('update:modelValue', JSON.parse(JSON.stringify(localFieldData.value)))
	}
	if (props.useDefault) {
		localFieldData.value = props.fieldConfig?.options?.defaultValue
	}
	const add = () => {
		if (!localFieldData.value) {
			localFieldData.value = []
		}
		localFieldData.value.push({})
		// localFieldData.value.unshift({})
		emits('update:modelValue', JSON.parse(JSON.stringify(localFieldData.value)))
	}
	const del = (index) => {
		localFieldData.value.splice(index, 1)
		emits('update:modelValue', JSON.parse(JSON.stringify(localFieldData.value)))
	}
	const submit = () => {
		const promiseList = []
		if (localFieldData.value) {
			localFieldData.value.forEach((item, index) => {
				promiseList.push(new Promise((resolve, reject) => {
					proxy.$refs[`batch-form-ref-${ props.fieldConfig.name }-${ index }`][0].validate().then(result => {
						resolve(result)
					}).catch(err => {
						reject(err)
					})
				}))
			})
		}
		return new Promise((resolve, reject) => {
			Promise.all(promiseList).then((result) => {
				resolve(result)
			}).catch(err => {
				reject(err)
			})
		})
	}
	defineExpose({
		submit
	})
</script>
<style>
</style>